package com.qf.rbac2202.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.rbac2202.sys.entity.SysDept;
import com.qf.rbac2202.sys.entity.SysUser;
import com.qf.rbac2202.sys.mapper.SysUserMapper;
import com.qf.rbac2202.sys.service.ISysDeptService;
import com.qf.rbac2202.sys.service.ISysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.rbac2202.utils.PageQueryUtils;
import com.qf.rbac2202.utils.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.awt.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 系统用户 服务实现类
 * </p>
 *
 * @author lzh
 * @since 2022-10-28
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    @Autowired
    ISysDeptService deptService;



    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public PageVo<SysUser> queryUserByPage(Map<String, String> params) {

        String username = params.get("username");//页面输入的用户查询条件


        //利用分页工具类创建分页
        Page<SysUser> sysUserPage = PageQueryUtils.getPage(params);


        //只查询有效的用户
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();

        //设置查询条件
        wrapper.eq("status",1);
        wrapper.eq(StringUtils.isNotBlank(username),"username", username);

        this.page(sysUserPage,wrapper);

        // 把所有user对象中的 deptName 进行赋值
        List<SysDept> deptList = deptService.list();


        //list<SysDept> --> map<部门id，部门名称>
        HashMap<Long, String> deptMap = new HashMap<>();

        deptList.forEach(sysDept -> deptMap.put(sysDept.getDeptId(),sysDept.getName()));

        sysUserPage.getRecords().forEach(sysUser -> sysUser.setDeptName(deptMap.get(sysUser.getDeptId())));

        return new PageVo<SysUser>(sysUserPage);


    }
}
